package com.mz.ozy.activity.me;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mz.ozy.R;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.presenter.VerifiedPresenter;
import com.mz.ozy.mvp.view.CodeContract;
import com.mz.ozy.mvp.view.VerfiedContract;
import com.mz.ozy.utils.CodeHelper;
import com.mz.ozy.utils.SPUtils;
import com.mz.ozy.utils.ToastUtils;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class VerifiedActivity extends BaseActivity<VerfiedContract.View, VerifiedPresenter> implements VerfiedContract.View, CodeContract.View {
    @BindView(R.id.et_real_name)
    EditText mEtRealName;
    @BindView(R.id.et_person_card)
    EditText mEtPersonCard;
    @BindView(R.id.tvGetCode)
    TextView tvGetCode;
    @BindView(R.id.codeEt)
    EditText codeEt;
    @BindView(R.id.llne)
    LinearLayout llne;
    @BindView(R.id.bt_confirm)
    Button btConfirm;
    @BindView(R.id.tv_speak)
    TextView tv_speak;

    private CodeHelper codeHelper;

    public static void go(Context context, String uname, String cardid, int shiming) {
        Intent intent = new Intent(context, VerifiedActivity.class);
        intent.putExtra("name", uname);
        intent.putExtra("shiming", shiming);
        intent.putExtra("cardId", cardid);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_verified);
        ButterKnife.bind(this);
        initTitle();
        tvTitle.setVisibility(View.GONE);
        tvCenterTitle.setText("实名认证");
        mEtRealName.setText(getIntent().getStringExtra("name"));
        mEtPersonCard.setText(getIntent().getStringExtra("cardId"));
        if (getIntent().getIntExtra("shiming", 0) == 1) {
            llne.setVisibility(View.GONE);
            btConfirm.setVisibility(View.GONE);
            tv_speak.setVisibility(View.VISIBLE);
            mEtRealName.setEnabled(false);
            mEtPersonCard.setEnabled(false);
        } else {
            tv_speak.setVisibility(View.GONE);
        }
        codeHelper = new CodeHelper(this, tvGetCode);
    }


    @OnClick({R.id.bt_confirm, R.id.tvGetCode})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvGetCode:
                mPresenter.onGetCode(this, SPUtils.getInstance(this).getString(Constants.SPName.USERNAME, ""));
                break;
            case R.id.bt_confirm:
                final String realName = mEtRealName.getText().toString().trim();
                final String personCard = mEtPersonCard.getText().toString().trim();
                final String code = codeEt.getText().toString().trim();
                if (TextUtils.isEmpty(realName)) {
                    ToastUtils.showToast(R.string.str_enter_real_name);
                    return;
                }
                if (TextUtils.isEmpty(personCard)) {
                    ToastUtils.showToast(R.string.str_enter_identity_card);
                    return;
                }
                if (TextUtils.isEmpty(code)) {
                    ToastUtils.showToast("请输入验证码");
                    return;
                }
                mPresenter.uploadHeader(this, realName, personCard, code);
                break;
        }
    }

    @Override
    public void onGetCodeSuccess(JSONObject jsonObject) {
        ToastUtils.showToast(getString(R.string.have_send_verify_code));
        codeHelper.start();  //开启倒计时
    }

    @Override
    public void onGetCodeFail() {
        codeHelper.reset();  //可以点击
    }

    @Override
    public void uploadSuccess(JSONObject bean) {
        ToastUtils.showToast("认证成功");
        finish();
    }
}
